/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameAntSystem.java
 *
 * Created on 27/08/2011, 12:24:49
 */

package gui;

import antsystem.AntSystem;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JTextField;

/**
 *
 * @author Larissa
 */
public class JFrameAntSystem extends javax.swing.JFrame implements Observer{

    /** Creates new form JFrameAntSystem */
    public JFrameAntSystem(AntSystem sistema) {
        this.sistema = sistema;
        this.sistema.addObserver((Observer) this);
        initComponents();
        update(this.sistema, null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextIteracoes = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextCoeficienteDecaimento = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextTamanhoOtimo = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextPercursoOtimo = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabelIteracao = new javax.swing.JLabel();
        jLabelQuantidadeCidadesFormigas = new javax.swing.JLabel();
        jLabelAlfa = new javax.swing.JLabel();
        jLabelBeta = new javax.swing.JLabel();
        jLabelQtdFormigasElitistas = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Ant System");

        jTextIteracoes.setText("10");

        jLabel2.setText("Iterações:");

        jLabel3.setText("Coeficiente de Decaimento:");

        jTextCoeficienteDecaimento.setText("0.5");
        jTextCoeficienteDecaimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextCoeficienteDecaimentoActionPerformed(evt);
            }
        });

        jLabel4.setText("Quantidade de Cidades/Formigas:");

        jLabel5.setText("Alfa:");

        jLabel6.setText("Beta:");

        jLabel7.setText("Formigas E:");

        jLabel8.setText("Tamanho");

        jLabel9.setText("Percurso");

        jLabel10.setText("Percurso Ótimo");

        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/imagens/formiguinhas.png"))); // NOI18N

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel11.setText("Iteração: ");

        jLabelIteracao.setText("0");

        jLabelQuantidadeCidadesFormigas.setText("30");

        jLabelAlfa.setText("1");

        jLabelBeta.setText("5");

        jLabelQtdFormigasElitistas.setText("5");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextPercursoOtimo, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabelIteracao)
                                        .addGap(141, 141, 141))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextTamanhoOtimo, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(81, 81, 81)
                                .addComponent(jLabel12)
                                .addGap(129, 129, 129))
                            .addComponent(jButton1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(284, 284, 284)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextCoeficienteDecaimento)
                                .addComponent(jTextIteracoes, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabelQuantidadeCidadesFormigas))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabelQtdFormigasElitistas))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelAlfa)
                                    .addComponent(jLabelBeta))))))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(48, 48, 48)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabelAlfa)))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextIteracoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextCoeficienteDecaimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6)
                                .addComponent(jLabelBeta)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabelQuantidadeCidadesFormigas)
                            .addComponent(jLabel7)
                            .addComponent(jLabelQtdFormigasElitistas))
                        .addGap(60, 60, 60)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabelIteracao))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jTextTamanhoOtimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel12)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextPercursoOtimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(35, 35, 35)
                .addComponent(jButton1)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextCoeficienteDecaimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextCoeficienteDecaimentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextCoeficienteDecaimentoActionPerformed



    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        

//        int iteracoes;
//        iteracoes = Integer.valueOf(jTextIteracoes.getText());
//        double coeficienteDecaimento = Double.valueOf(jTextCoeficienteDecaimento.getText());
//        int qtdCidadesFormigas = Integer.valueOf(jTextQuantidadeCidadesFormigas.getText());
//        int alfa = Integer.valueOf(jTextAlfa.getText());
//        int beta = Integer.valueOf(jTextBeta.getText());
//        int qtdFormigasElitistas = Integer.valueOf(jTextQuantidadeElitistas.getText());
//        AntSystem sistema = new AntSystem(iteracoes, qtdCidadesFormigas, coeficienteDecaimento, alfa, beta, qtdFormigasElitistas );
//
        //---------------------
        int iteracoes;
        iteracoes = Integer.valueOf(jTextIteracoes.getText());
        double coeficiente;
        coeficiente = Double.valueOf(jTextCoeficienteDecaimento.getText());
        jTextTamanhoOtimo.setText("");
        jTextPercursoOtimo.setText("");
        sistema.setQtdIteracoes(iteracoes);
        sistema.setCoeficiente_decaimento(coeficiente);
        //sistema = new AntSystem(iteracoes, this);
        //sistema.menorCaminho();

        if(processo==null || processo.isInterrupted()) { //Instancia a thread SE não existir uma
            processo = new Thread(sistema);
            processo.start();
        } else {
            System.out.println("O processo ainda está em execução");
        }

//        System.out.println("Tamanho ótimo: " + sistema.getTamanhoPercurssoOtimo());
//        System.out.print("Percursso ótimo: ");
//        String tudo ="";
//        for(int i=0; i<AntSystem.QUANTIDADE_CIDADES_DEFAULT+1; i++){
//            //System.out.print(sistema.getPercurssoOtimo()[i] + " , ");
//            if(i<AntSystem.QUANTIDADE_CIDADES_DEFAULT)
//                tudo += sistema.getPercurssoOtimo()[i] + " , ";
//            else
//                tudo += sistema.getPercurssoOtimo()[i];
//        }
//        jTextTamanhoOtimo.setText(String.valueOf(sistema.getTamanhoPercurssoOtimo()));
//        jTextPercursoOtimo.setText(tudo);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    public void setTamanhoOtimo(String tamanho)
    {
        jTextTamanhoOtimo.setText(String.valueOf(tamanho));
    }

    public void setPercursoOtimo(String percurso)
    {
        jTextPercursoOtimo.setText(percurso);
    }

    public void finalizarProcessoTreino()
    {
        processo.interrupt();
        if (processo.isInterrupted()) {
        processo = null;
        }
    }


    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new JFrameAntSystem().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelAlfa;
    private javax.swing.JLabel jLabelBeta;
    private javax.swing.JLabel jLabelIteracao;
    private javax.swing.JLabel jLabelQtdFormigasElitistas;
    private javax.swing.JLabel jLabelQuantidadeCidadesFormigas;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextCoeficienteDecaimento;
    private javax.swing.JTextField jTextIteracoes;
    private javax.swing.JTextField jTextPercursoOtimo;
    private javax.swing.JTextField jTextTamanhoOtimo;
    // End of variables declaration//GEN-END:variables
    private Thread processo;
    private AntSystem sistema;

    public void update(Observable o, Object arg) {
        jLabelIteracao.setText(String.valueOf(sistema.getIteracaoAtual()));
    }

}
